#include "wtlpch.h"

#include "ErrorHandler.h"

#define MSG_TCHAR_BUFFER 256		//Define the message buffer size

CErrorHandler::CErrorHandler(void)
{
}

CErrorHandler::~CErrorHandler(void)
{
}

int CErrorHandler::ShowMessageNoParam(HWND hWnd, UINT uiTitleString, UINT uiInfoString, UINT uType)
{
	//Load String from resouce
	LPWSTR lpwstrMsg = new WCHAR[MSG_TCHAR_BUFFER];
	LPWSTR lpwstrMsgTitle = new WCHAR[MSG_TCHAR_BUFFER];

	::wmemset(lpwstrMsg, 0, MSG_TCHAR_BUFFER);
	::wmemset(lpwstrMsgTitle, 0, MSG_TCHAR_BUFFER);

	//We assume use the same module
	::LoadString(::GetModuleHandle(NULL), uiTitleString, lpwstrMsgTitle, MSG_TCHAR_BUFFER);
	::LoadString(::GetModuleHandle(NULL), uiInfoString, lpwstrMsg, MSG_TCHAR_BUFFER);

	int nRet = ::MessageBox(hWnd, lpwstrMsg, lpwstrMsgTitle, uType);

	//Delete the buffer we allocated
	delete lpwstrMsg;
	delete lpwstrMsgTitle;

	return nRet;
}

int CErrorHandler::ShowMessageWithParam(HWND hWnd, UINT uiTitleString, UINT uiInfoString, UINT uType, UINT uiParamCount, ...)
{
	//Load String from resouce
	LPWSTR lpwstrMsg = new WCHAR[MSG_TCHAR_BUFFER];
	LPWSTR lpwstrMsgTitle = new WCHAR[MSG_TCHAR_BUFFER];

	//Prepare Msg String
	LPWSTR lpwstrMsgBuff = new WCHAR[MSG_TCHAR_BUFFER];

	::wmemset(lpwstrMsg, 0, MSG_TCHAR_BUFFER);
	::wmemset(lpwstrMsgTitle, 0, MSG_TCHAR_BUFFER);
	::wmemset(lpwstrMsgBuff, 0, MSG_TCHAR_BUFFER);

	//We assume use the same module
	::LoadString(::GetModuleHandle(NULL), uiTitleString, lpwstrMsgTitle, MSG_TCHAR_BUFFER);
	::LoadString(::GetModuleHandle(NULL), uiInfoString, lpwstrMsg, MSG_TCHAR_BUFFER);

	//Read the var params
	va_list paraList;
	va_start(paraList, uiParamCount);
	
	PARAM_TYPE pt;
	LPCWSTR psa, psb;
	INT		pia, pib;
	//Got the param size
	switch(uiParamCount)
	{
	case 1:

		pt = va_arg(paraList, PARAM_TYPE);
		
		if(pt == TYPE_INT)
		{
			::wsprintfW(lpwstrMsgBuff, lpwstrMsg, va_arg(paraList, INT));
		}
		else if(pt == TYPE_WSTR)
		{
			::wsprintfW(lpwstrMsgBuff, lpwstrMsg, va_arg(paraList, LPCWSTR));
		}

		break;

	case 2:
		
		pt = va_arg(paraList, PARAM_TYPE);

		//For Param 1
		if(pt == TYPE_INT)
		{
			pia = va_arg(paraList, INT);

			pt = va_arg(paraList, PARAM_TYPE);

			if(pt == TYPE_INT)
			{
				pib = va_arg(paraList, INT);
				::wsprintfW(lpwstrMsgBuff, lpwstrMsg, pia, pib);
			}
			else if(pt == TYPE_WSTR)
			{
				psb = va_arg(paraList, LPCWSTR);
				::wsprintfW(lpwstrMsgBuff, lpwstrMsg, pia, psb);
			}
		}
		else if(pt == TYPE_WSTR)
		{
			psa = va_arg(paraList, LPCWSTR);

			pt = va_arg(paraList, PARAM_TYPE);

			if(pt == TYPE_INT)
			{
				pib = va_arg(paraList, INT);
				::wsprintfW(lpwstrMsgBuff, lpwstrMsg, psa, pib);
			}
			else if(pt == TYPE_WSTR)
			{
				psb = va_arg(paraList, LPCWSTR);
				::wsprintfW(lpwstrMsgBuff, lpwstrMsg, psa, psb);
			}
		}

		break;
	}

	va_end(paraList);

	//Show the message box
	int nRet = ::MessageBox(hWnd, lpwstrMsgBuff, lpwstrMsgTitle, uType);

	//Delete the buffer we allocated
	delete lpwstrMsg;
	delete lpwstrMsgTitle;
	delete lpwstrMsgBuff;

	return nRet;
}
